<template>
  <div>
    <van-nav-bar
      title="个人信息"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-field v-model="info.nickname" label="昵称" style="margin-top: 20px" />
    <van-field v-model="info.sex" label="性别" />
    <van-field v-model="info.Introduction" label="简介" />
    <p class="photo">个人头像</p>
    <div class="img" style="display: flex">
      <van-image
        width="100"
        height="100"
        :src="info.img_url"
        style="margin-bottom: 20px"
      />
      <van-uploader :after-read="afterRead">
        <van-button icon="plus" type="primary">修改头像</van-button>
      </van-uploader>
    </div>
    <van-button round block type="info" @click="updataInfo">更新信息</van-button>
  </div>
</template>

<script>
import { detailInfo, updateInfo } from '@api/home'
import { Toast } from 'vant'
export default {
  name: 'myinfo',
  data () {
    return {
      info: {
        nickname: '',
        sex: '',
        Introduction: '',
        img_url: ''
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      detailInfo().then((res) => {
        if (res.data.code === 200) {
          this.info = res.data.data
        }
      })
    },
    afterRead (file) {
      const param = new FormData()
      param.append('file', file.file)
      this.$http.post('http://localhost:3000/api/phone/updateImg', param, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.code === 200) {
          this.info.img_url = 'http://localhost:3000' + res.data.data
        }
      })
    },
    updataInfo () {
      updateInfo(this.info).then((res) => {
        if (res.data.code === 200) {
          Toast.success(res.data.text)
          this.init()
          this.$router.push('/user')
        }
      })
    },
    onClickLeft () {
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
.photo {
  padding: 15px;
  font-size: 14px;
}
.van-image {
  margin-left: 40px;
}
.van-uploader {
  margin-left: 100px;
}
</style>
